﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure;

namespace PracticeAzure.Website.Shared
{
    public sealed class GlobalConfiguration
    {
        public static bool DebugMode { get; private set; }
        public static int PageSize { get; private set; }
        public static int BioLength { get; private set; }
        public static string AttachementContainerName { get; private set; }
        public static string AccountName { get; private set; }
        public static string AccountKey { get; private set; }
        public static string AttachmentNameFormat { get; private set; }
        public static bool AllowReply { get; private set; }
        public static bool PermanentDelete { get; private set; }
        public static int CacheExpirationInSeconds { get; private set; }
        public static string ChaptersContainerName { get; private set; }
        public static string SamplesContainerName { get; private set; }

        public static Uri BlobRootUrl { get; private set; }

        static GlobalConfiguration()
        {
            // debug mode
            var debugMode = false;
            bool.TryParse(RoleEnvironment.GetConfigurationSettingValue("DebugMode"), out debugMode);
            DebugMode = debugMode;

            // page size
            var pageSize = 20;
            int.TryParse(RoleEnvironment.GetConfigurationSettingValue("PageSize"), out pageSize);
            PageSize = pageSize;

            // bio length
            var bioLength = 100;
            int.TryParse(RoleEnvironment.GetConfigurationSettingValue("BioLength"), out bioLength);
            BioLength = bioLength;

            // attachements container name
            AttachementContainerName = RoleEnvironment.GetConfigurationSettingValue("AttachementContainerName");

            // account name and key
            AccountName = RoleEnvironment.GetConfigurationSettingValue("AccountName");
            AccountKey = RoleEnvironment.GetConfigurationSettingValue("AccountKey");

            // attachment name format string
            AttachmentNameFormat = RoleEnvironment.GetConfigurationSettingValue("AttachmentNameFormat");

            // debug mode
            var allowReply = false;
            bool.TryParse(RoleEnvironment.GetConfigurationSettingValue("AllowReply"), out allowReply);
            AllowReply = allowReply;

            // debug mode
            var permanentDelete = false;
            bool.TryParse(RoleEnvironment.GetConfigurationSettingValue("PermanentDelete"), out permanentDelete);
            PermanentDelete = permanentDelete;

            // CacheExpirationInSeconds
            var cacheExpirationInSeconds = 3;
            int.TryParse(RoleEnvironment.GetConfigurationSettingValue("CacheExpirationInSeconds"), out cacheExpirationInSeconds);
            CacheExpirationInSeconds = cacheExpirationInSeconds;

            // chapters and samples container name
            ChaptersContainerName = RoleEnvironment.GetConfigurationSettingValue("ChaptersContainerName");
            SamplesContainerName = RoleEnvironment.GetConfigurationSettingValue("SamplesContainerName");

            // blob root url
            var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            BlobRootUrl = account.BlobEndpoint;
        }

        public static string GetBlobAbsoluteUrl(string container, string blob)
        {
            return string.Format("{0}/{1}/{2}", BlobRootUrl.AbsoluteUri.TrimEnd('/'), container, blob);
        }
    }
}
